package com.artron.mediaartron.data.config;

import android.app.Activity;
import android.content.Context;

import com.artron.baselib.utils.UIUtils;
import com.artron.mediaartron.data.entity.LoginData;
import com.artron.mediaartron.data.entity.User;
import com.artron.mediaartron.ui.activity.LoginActivity;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

import rx.functions.Action0;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-10-11.
 */

public class Altar {

    private static Altar altar = null;
    private Altar(){}
    static Altar getInstance() {
        if (altar == null) {
            synchronized (Altar.class) {
                if (altar == null) {
                    altar = new Altar();
                }
            }
        }
        return altar;
    }

    private User user;

    public User getUser() {
        if (user == null) {
            try {
                user = AppProfile.getUserManager().getUser();
            } catch (Exception e) {
                return null;
            }
        }
        return user;
    }

    public String getPassId() {
        User user = getUser();
        return user == null ? "" : user.getPassId();
    }

    public boolean isLogin() {
        User user = getUser();
        return user != null;
    }

    public void startLoginActivity(Context context, Action0 callback) {
        if (!isLogin()) {
            LoginActivity.start(context, LoginActivity.TYPE_LOGIN);
        } else {
            callback.call();
        }
    }

    public void login(LoginData data) {
        this.user = new User(data);
        try {
            AppProfile.getUserManager().saveUser(user);
        } catch (Exception e) {
            MobclickAgent.reportError(UIUtils.getContext(), e);
        }
    }

    public void update(LoginData data) {
        this.user.update(data);
        try {
            AppProfile.getUserManager().saveUser(user);
        } catch (Exception e) {
            MobclickAgent.reportError(UIUtils.getContext(), e);
        }
    }

    public void update() {
        try {
            AppProfile.getUserManager().saveUser(user);
        } catch (Exception e) {
            MobclickAgent.reportError(UIUtils.getContext(), e);
        }
    }

    public void logout() {
        if (this.user != null) {
            AppProfile.getUserManager().deleteUser();
            this.user = null;
        }
    }

    /**
     * 注销三方平台账号登录，要不然每次都不提示就自动登录，想切换账号都切换不了；
     *
     * @param activity
     * @param platform
     */
    public void logoutPlatform(Activity activity, SHARE_MEDIA platform) {
        if (this.user != null) {

            UMShareAPI mUMShareAPI = UMShareAPI.get(activity);
            mUMShareAPI.deleteOauth(activity, platform, new UMAuthListener() {
                @Override
                public void onStart(SHARE_MEDIA share_media) {

                }

                @Override
                public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {

                }

                @Override
                public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {

                }

                @Override
                public void onCancel(SHARE_MEDIA share_media, int i) {

                }
            });
            AppProfile.getUserManager().deleteUser();
            this.user = null;
        }
    }
}
